# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=lxml
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=3.6.0
pkgrel=1
arch=('any')
license=('BSD' 'custom')
url="http://lxml.de/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python2-cssselect"
             "${MINGW_PACKAGE_PREFIX}-python3-cssselect")
source=(https://pypi.python.org/packages/source/l/lxml/${_realname}-${pkgver}.tar.gz{,.asc}
        mingw-python-fix.patch
        use-distutils-get_platform.patch)
sha256sums=('9c74ca28a7f0c30dca8872281b3c47705e21217c8bc63912d95c9e2a7cac6bdf'
            'SKIP'
            '66924a961e8baaa5dac4c3d8b72e4bb956f967025301a0320575c98ae9fab1c3'
            'e50ea7f6004596ab43fe77abbbf28db5ca3a3e9ff7936b4319799ce49362029c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/mingw-python-fix.patch
  patch -p1 -i ${srcdir}/use-distutils-get_platform.patch

  cd "${srcdir}"

  cp -r ${_realname}-${pkgver} build-python2
  cp -r ${_realname}-${pkgver} build-python3
}

build() {
  # It seems to me that CFLAGS and LDFLAGS aren't getting
  # converted to Windows versions when calling Python for
  # some reason?

  # Tried $(cygpath -m) to get -IC:/msys64/mingw32/
  # but that gets mangled to -IC;C:msys64msys64mingw32
  # PREFIX_WIN=$(cygpath -m $MINGW_PREFIX)
  # CPPFLAGS="$CPPFLAGS -I${PREFIX_WIN}/include/libxml2"
  # LDFLAGS="$LDFLAGS -L${PREFIX_WIN}/lib"

  # This horrible version works; needs the right amount of \\ to
  # escape the escapes. This stuff makes some of xslt-config
  # redundant (xslt-config *should* return C:/ paths but doesn't)
  PREFIX_WIN=$(cygpath -w $MINGW_PREFIX)
  PREFIX_WIN=${PREFIX_WIN//\\/\\\\}
  CPPFLAGS="$CPPFLAGS -I${PREFIX_WIN}\\\\include\\\\libxml2"
  LDFLAGS="$LDFLAGS -L${PREFIX_WIN}\\\\lib"

  # Build python 2 module
  cd "${srcdir}"/build-python2
  ${MINGW_PREFIX}/bin/python2 setup.py build --with-xslt-config=${MINGW_PREFIX}/bin/xslt-config

  # Build python 3 module
  cd "${srcdir}"/build-python3
  ${MINGW_PREFIX}/bin/python3 setup.py build --with-xslt-config=${MINGW_PREFIX}/bin/xslt-config
}

package_python2-lxml() {
  pkgdesc="Python2 binding for the libxml2 and libxslt libraries"
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-libxslt")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-beautifulsoup3: support for parsing not well formed HTML"
      "${MINGW_PACKAGE_PREFIX}-python-lxml-docs: offline docs")

  cd "${srcdir}/build-python2"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSES.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/elementtree.txt
}

package_python3-lxml() {
  pkgdesc="Python3 binding for the libxml2 and libxslt libraries"
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-libxslt")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-lxml-docs: offline docs")

  cd "${srcdir}/build-python3"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSES.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/elementtree.txt
}

package_python-lxml-docs() {
  pkgdesc="Python binding for the libxml2 and libxslt libraries (docs)"
  options=('docs')

  cd ${_realname}-$pkgver
  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
  cp -r doc/html "${pkgdir}${MINGW_PREFIX}"/share/doc/${_realname}
}

package_mingw-w64-i686-python2-lxml() {
  package_python2-lxml
}

package_mingw-w64-i686-python3-lxml() {
  package_python3-lxml
}

package_mingw-w64-x86_64-python2-lxml() {
  package_python2-lxml
}

package_mingw-w64-x86_64-python3-lxml() {
  package_python3-lxml
}

package_mingw-w64-i686-python-lxml-docs() {
  package_python-lxml-docs
}

package_mingw-w64-x86_64-python-lxml-docs() {
  package_python-lxml-docs
}
